package com.comon.core.domain.result;

import com.comon.core.enums.ResultCode;
import lombok.Data;

import java.io.Serializable;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-08-24
 * Time:15:29
 * Text：
 */
@Data
public class R<T> implements Serializable {
    /**
     * 消息状态码
     */
    private int code;

    /**
     * 消息内容
     */
    private String msg;

    /**
     * 数据对象
     */
    private T data;


    private static <T> R<T> assembleResult(T data, ResultCode resultCode) {
        R<T> r = new R<>();
        r.setCode(resultCode.getCode());
        r.setData(data);
        r.setMsg(resultCode.getMsg());
        return r;
    }

    public static <T> R<T> ok() {
        return assembleResult(null, ResultCode.SUCCESS);
    }
    public static <T> R<T> ok(String msg) {
        return assembleResult(null, 1000,msg);
    }
    public static <T> R<T> ok(String msg, T data) {
        return assembleResult(data, ResultCode.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> ok(T data) {
        return assembleResult(data, ResultCode.SUCCESS);
    }
    private static <T> R<T> assembleResult(T data, int code, String msg) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }

    public static <T> R<T> fail(int code, String msg) {
        return assembleResult(null, code, msg);
    }
    public static <T> R<T> fail(ResultCode resultCode) {
        return assembleResult(null,resultCode.getCode(),resultCode.getMsg() );
    }

    public static <T> R<T> fail(String msg) {
        return assembleResult(null,ResultCode.FAILED.getCode(),msg);
    }

    public static <T> R<T> fail(String msg, T data) {
        return assembleResult(data, ResultCode.FAILED.getCode(), msg);
    }

}
